package com.loveprogrammer.eternity.model.game;

import com.loveprogrammer.eternity.model.Hero;
import com.loveprogrammer.eternity.model.mission.MissionDefinition;
import com.loveprogrammer.eternity.model.mission.support.KillMonsterMission;
import com.loveprogrammer.eternity.model.task.TaskDefinition;
import com.loveprogrammer.eternity.task.MainTaskService;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

/**
 * 任务管理类
 * @author lqhao
 */
public class TaskManager {

    private List<TaskDefinition> allTasks = new ArrayList<>();

    // 已经获得了收益的任务
    private ConcurrentHashMap<String, TaskDefinition> hisTasks =new ConcurrentHashMap<>();

    // 已经完成但是还没有获得收益的任务
    private ConcurrentHashMap<String, TaskDefinition> waitingToBeDeliveredTasks =new ConcurrentHashMap<>();

    /**
     * 已经完成的任务列表
     */
    private List<TaskDefinition> completeTasks = new ArrayList<>();

    /***
     * 总任务的map 待完成的任务
     */
    private ConcurrentHashMap<String, TaskDefinition> taskBeans = new ConcurrentHashMap<>();

    public ConcurrentHashMap<String, TaskDefinition> getTaskBeans() {
        return taskBeans;
    }

    /***
     * 击杀野怪的任务
     */
    public ConcurrentHashMap<String, TaskDefinition> killMonsterTasks = new ConcurrentHashMap<>();


    public void setTaskBeans(ConcurrentHashMap<String, TaskDefinition> taskBeans) {
        this.taskBeans = taskBeans;
    }

    /**
     * 增加任务
     * @return
     */
    public void addTask(TaskDefinition taskBean){

        taskBeans.put(taskBean.getTaskId(),taskBean);
        MissionDefinition mission = taskBean.getMission();
        // 击杀任务
        if(mission instanceof KillMonsterMission){
            killMonsterTasks.put(String.valueOf(((KillMonsterMission) mission).getMonsterId()),taskBean);
        }
        allTasks.add(taskBean);
    }

    /**
     * 移除任务
     * @param taskMessageId
     */
//    public void removeTask(String taskMessageId){
//        taskBeans.remove(taskMessageId);
//        waitingToBeDeliveredTasks.remove(taskMessageId);
//        hisTasks.remove(taskMessageId);
//
//
//    }

    /**
     * 所有任务更新
     * @param taskAction
     * @param hero
     */
    public void handler(TaskDefinition taskAction, Hero hero){
//        for (TaskDefinition task : taskBeans.values()) {
//            Mission mission = task.getMission();
//            Integer taskTargetType = mission.get();
//            if (taskTargetType.equals(taskAction.getTaskTargetType()) && mission.getStatus().equals(TaskStateEnum.ON_DOING.getCode())){
//                mission.update(hero, taskAction);
//            }
//        }
    }

    /**
     * 返回任务列表Ids
     */
    public List<String> getTaskIds(){
        return allTasks.stream().map(TaskDefinition::getTaskId).collect(Collectors.toList());
    }

    public ConcurrentHashMap<String, TaskDefinition> getKillMonsterTasks() {
        return killMonsterTasks;
    }

    public void complete(TaskDefinition taskDefinition, MainTaskService mainTaskService) {
        taskDefinition.setStatus(3);
        completeTasks.add(taskDefinition);
        // 等待获取奖励的列表
        waitingToBeDeliveredTasks.put(taskDefinition.getTaskId(),taskDefinition);
        // 完成任务
        mainTaskService.complete(taskDefinition.getTaskId());
        // 从待完成任务列表中去除
        taskBeans.remove(taskDefinition.getTaskId());

        MissionDefinition mission = taskDefinition.getMission();
        if(mission instanceof KillMonsterMission) {
            killMonsterTasks.remove(String.valueOf(((KillMonsterMission) mission).getMonsterId()));
        }

        // 修改
    }

    /***
     * 已经领取了任务奖励
     * @param taskDefinition
     */
    public void completeClaim(TaskDefinition taskDefinition) {
        // 从待领取去除 加入历史任务
        waitingToBeDeliveredTasks.remove(taskDefinition.getTaskId());
        hisTasks.put(taskDefinition.getTaskId(),taskDefinition);
    }

    public ConcurrentHashMap<String, TaskDefinition> getHisTasks() {
        return hisTasks;
    }

    public ConcurrentHashMap<String, TaskDefinition> getWaitingToBeDeliveredTasks() {
        return waitingToBeDeliveredTasks;
    }

    public List<TaskDefinition> getCompleteTasks() {
        return completeTasks;
    }

    public List<TaskDefinition> getAllTasks() {
        return allTasks;
    }

    public void setAllTasks(List<TaskDefinition> allTasks) {
        this.allTasks = allTasks;
    }
}
